package api

import (
	"OJCenter/global"
	"OJCenter/idl"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"github.com/google/uuid"
	"net/http"
)

// @Summary	添加题目
// @Produce	json
// @Param		pro	body		idl.Problem		true	"题目信息"
// @Success	200	{object}	global.Response	""
// @Fail		400  {object} global.Response ""
// @Router		/api/v1/createProblem [post]
func (a *Api) CreateProblem(c *gin.Context) {
	var Problem idl.Problem

	err := c.ShouldBindJSON(&Problem)
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, err)
		return
	}

	validate := validator.New()
	err = validate.Struct(&Problem)
	if err != nil {
		global.MarkErrors(err.(validator.ValidationErrors))
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, nil)
		return
	}

	Uuid, _ := uuid.NewRandom()
	Problem.Pid = int64(Uuid.ID())

	err = a.M.CreateProblem(&Problem)
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, err)
		return
	}

	global.Res(c, http.StatusOK, global.SUCCESS, new(interface{}))
}

// @Summary	删除题目
// @Produce	json
// @Param		pids	body		[]int			true	"题目ID"
// @Success	200		{object}	global.Response	""
// @Fail		400  {object} global.Response{Data=[]int64} ""
// @Router		/api/v1/deleteProblem [post]
func (a *Api) DeleteProblem(c *gin.Context) {
	var pids []int64

	err := c.ShouldBindJSON(&pids)
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, err)
		return
	}

	sum, fu := a.M.DeleteProblems(pids)
	if sum != len(pids) {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, fu)
		return
	}
	global.Res(c, http.StatusOK, global.SUCCESS, new(interface{}))
}

// @Summary	更新题目
// @Produce	json
// @Param		pros	body		[]idl.Problem	true	"题目信息"
// @Success	200		{object}	global.Response	""
// @Fail		400  {object} global.Response{Data=[]idl.Problem} ""
// @Router		/api/v1/updateProblem [post]
func (a *Api) UpdateProblem(c *gin.Context) {
	var Problem []idl.Problem

	err := c.ShouldBindJSON(&Problem)
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, err)
		return
	}

	validate := validator.New()
	for _, t := range Problem {
		err := validate.Struct(&t)
		if err != nil {
			global.MarkErrors(err.(validator.ValidationErrors))
			global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, nil)
			return
		}
	}

	sum, fu := a.M.UpdateProblems(&Problem)
	if sum != len(Problem) {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, fu)
		return
	}
	global.Res(c, http.StatusOK, global.SUCCESS, new(interface{}))
}

// @Summary	获取题目列表
// @Produce	json
// @Param		page	query		int									true	"页码"
// @Param		limit	query		int									true	"限制"
// @Success	200		{object}	global.Response{Data=[]idl.Problem}	""
// @Fail		400  {object} global.Response ""
// @Router		/api/v1/getProblemList [Get]
func (a *Api) GetProblemList(c *gin.Context) {
	var Problem idl.Problem

	err := c.ShouldBindJSON(&Problem)
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, err)
		return
	}

	validate := validator.New()
	err = validate.Struct(&Problem)
	if err != nil {
		global.MarkErrors(err.(validator.ValidationErrors))
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, nil)
		return
	}

	sum, err := a.M.SelectProblem(Problem)
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, nil)
		return
	}
	global.Res(c, http.StatusOK, global.SUCCESS, sum)
}
